import { Component, Input, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { SHOP_AUDITING_STATUS_OPTION, YESNO_OPTION } from '../../../constant/dictionary';
import { myValidNull } from '@core/util/string-utils';
import { NzMessageService } from 'ng-zorro-antd';
import { OtcShopService } from '@core/routes-services/otc-shop.service';
import { OtcShopEditComponent } from '../otc-shop-edit/otc-shop-edit.component';
import { AuditingComponent } from '../../auditing/auditing.component';
import { OtcShopShopCreditRuleComponent } from '../shop-credit-rule/shop-credit-rule.component';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-otc-shop-list',
  templateUrl: './otc-shop-list.component.html',
  styles: [`
    a {
      color: #40a9ff
    }
  `],
})
export class OtcShopListComponent implements OnInit {


  @Input() centerId: any;
  isLoading = false;
  keyword: any;
  staus: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  yesOrNo: any [] = YESNO_OPTION;
  shopAuditingStatus: any [] = SHOP_AUDITING_STATUS_OPTION;

  // 公众号二维码图ticket
  //  访问方式 ： https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
  wxPublicImgDomain = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=';

  constructor(private router: Router,
              private message: NzMessageService,
              private otcShopService: OtcShopService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.otcShopService.pagination(this.page, this.keyword, this.staus, this.centerId).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OtcShopEditComponent, { entity: bak }, 660, {
      nzTitle: '店铺信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  seeLog = (data) => {
    let url = '/otc-shop/list/log';
    if (location.hash.indexOf('detail') >= 0) {
      url = '/otc-center/list/detail/log';
    }
    this.router.navigate([url], { queryParams: { shopId: data.id, brandId: data.brandId } });
  };

  /**
   * 营业与否
   * @param data
   */
  onsale = (data) => {
    this.otcShopService.onsaleEntity(data.id).then((data) => {
      this.search();
      this.message.success('操作成功');
    }, error => console.log('操作出错，后续处理'));
  };
  confirmAuditing = (entity) => {
    this.otcShopService.confirmAuditing(entity.id).then((data) => {
      this.changePageNo(this.page.number);
      this.message.success('操作成功');
    }, error => console.log('操作出错，后续处理'));
  };

  rejectAuditing = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(AuditingComponent, { entity: bak, businessService: this.otcShopService }, 360, {
      nzTitle: '驳回店铺',
    }).subscribe(result => {
      if (result) {
        this.message.success('驳回成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
  setShopRule = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OtcShopShopCreditRuleComponent, { entity: bak }, 1100, {
      nzTitle: entity.title + '的奖励规则',
    }).subscribe(result => {
      if (result) {
        this.message.success('设置成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
}
